// ==============================================================================
//
// Software Development Kit v 3.0
// file api unit
//
// Copyright (c) 2008-2009 Gear Pair Games
//
// Copyright and license exceptions noted in source
//
// Description: This module is designed to work with file operations.
// ==============================================================================
(*
history:
 - [ 15 aug 09 ] module create, d.martynenko
*)
unit fileApi;

interface

uses
  types,
  structs,
  headers,
  enums,
  os;

type
// CFileStream
  CFileStream = class( IFileStream )
  private
    feof : bool;
    fmode : enumOfFileMode;
    fhandle : intRec;
    fsize, frbyte : uint;
  public
// CFileStream::open
    function Open( const inFile : ansiString; const inMode : enumOfFileMode = [ ] ) : bool; overload;
// CFileStream::open
    function Open( const inHandle, inSize : handle; const inMode : enumOfFileMode = [ ] ) : bool; overload;
// CFileStream::read
    procedure read( const inBuffer : void; const inSize : uint ); override;
// CFileStream::get
    function get( const inSize : uint ) : void;
// CFileStream::write
    procedure write( const inBuffer : void; const inSize : uint ); override;
// CFileStream::close
    procedure close; override;
// CFileStream::getSize
    function getSize : int; override;
  end;

  // CFileStream
  CTextFile = class( CFileStream )
  public
    // need free mem!
    function readText : ansiString;
    procedure writeText( inText : ansiString );
  end;

implementation

uses
  utils;

{ CFileStream }

// CFileStream::open
function CFileStream.Open( const inFile : ansiString; const inMode : enumOfFileMode = [ ] ) : bool;
var
  filehndl : handle;
begin
  result := false;
  filehndl := osFileGet( inFile, fmCreate in inMode );
  if filehndl = invalidHandle then
    exit;
  // open and exit
  result := ( Open( filehndl, osFileSize( filehndl ), inMode ) );
end;

// CFileStream::Open
function CFileStream.Open( const inHandle, inSize : handle; const inMode : enumOfFileMode = [ ] ) : bool;
begin
  fhandle.asHandle := inHandle;
  fmode := inMode;
  fsize := inSize;
  feof := false;
  result := true;
end;

// CFileStream::close
procedure CFileStream.close;
begin
  if fhandle.asInt <> -1 then
    osHndlClose( fhandle.asHandle );
  free;
end;

// CFileStream::get
function CFileStream.get( const inSize : uint ) : void;
begin
  getmem( result, inSize );
  inc( frbyte, osFileRead( fhandle.asHandle, inSize, result^, feof ) );
end;

// CFileStream::getSize
function CFileStream.getSize : int;
begin
  result := ( osFileSize( fhandle.asHandle ) );
end;

// CFileStream::read
procedure CFileStream.read( const inBuffer : void; const inSize : uint );
begin
  inc( frbyte, osFileRead( fhandle.asHandle, inSize, inBuffer^, feof ) );
end;

// CFileStream::write
procedure CFileStream.write( const inBuffer : void; const inSize : uint );
begin
  inc( frbyte, osFileWrite( fhandle.asHandle, inSize, inBuffer^, feof ) );
end;

{ CFileStream:: }

// fileStream::CTextFile::readText
function CTextFile.readText : ansiString;
var
  p : pchar;
begin
  p := get( fsize );
  SetString( result, p, fsize );
  freemem( p, fsize );
end;

// fileStream::CTextFile::writeText
procedure CTextFile.writeText( inText : ansiString );
begin
  write( pchar( inText ), strLn( inText ) );
end;

end.
